﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WanMeiBussinessService;
using WanMeiCommon;
using WanMeiModel;

namespace WanMeiService
{
    public class UserController
    {
        private static IUserService _IUserSerivce = BussinessFactory.CreateInstance<IUserService>("UserService");
        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ControllerStatus Register(UserRegisterModel model, out string msg)
        {
            var result = ControllerStatus.error;
            if (string.IsNullOrEmpty(model.UserName) || string.IsNullOrEmpty(model.PassWord) || string.IsNullOrEmpty(model.RePassWord))
            {
                msg = "前三行数据不能为空";
                return result;
            }
            if (model.UserName.Length < 6 || model.PassWord.Length < 6)
            {
                msg = "账号和密码长度不能小于6";
                return result;
            }
            if (model.PassWord != model.RePassWord)
            {
                msg = "两次密码不一致";
                return result;
            }
            int returnValue = _IUserSerivce.Register(model);
            switch (returnValue)
            { 
                case 1:
                    result = ControllerStatus.success;
                    msg = "";
                    break;
                case 2:
                    msg = "已存在用户";
                    break;
                default:
                    msg = "注册出错!";
                    break;
            }
            return result;
        }

        public ControllerStatus Login(UserLoginModel model,out string msg)
        {
            var result = ControllerStatus.error;
            if (string.IsNullOrEmpty(model.UserName) || string.IsNullOrEmpty(model.PassWord))
            {
                msg = "账号和密码不能为空";
                return result;
            }
            if (model.UserName.Length < 6 || model.PassWord.Length < 6)
            {
                msg = "账号和密码长度不能小于6";
                return result;
            }
            int returnValue = _IUserSerivce.Login(model);
            switch (returnValue)
            {
                case 1:
                    result = ControllerStatus.success;
                    msg = "";
                    break;
                case 2:
                    msg = "用户不存在";
                    break;
                default:
                    msg = "账号或密码错误!";
                    break;
            }
            return result;
        }
        /// <summary>
        /// 检测用户名是否存在
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public bool CheckUserIsExist(string username)
        {
            return _IUserSerivce.IsExistUser(SecretClass.DecryptQueryString(username));
        }
    }
}
